package com.zhiwang.planet.ui.gamebase

import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.widget.AppCompatTextView
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestManager
import com.zhiwang.planet.R
import com.zhiwang.planet.di.component.DaggerPayComponent
import com.zhiwang.planet.di.module.AccountModule
import com.zhiwang.planet.di.module.AutoDisposeModule
import com.zhiwang.planet.mvp.p.ScalarPresenter
import com.zhiwang.planet.mvp.p.UserPresenter
import com.zhiwang.planet.mvp.p.VipPresenter
import com.zhiwang.planet.mvp.v.GameListView
import com.zhiwang.planet.mvp.v.UserInfoView
import com.zhiwang.planet.mvp.v.VipView
import com.zhiwang.planet.mvvm.model.StringHolder
import com.zhiwang.planet.mvvm.model.entity.User
import com.zhiwang.planet.mvvm.model.entity.VipModel
import com.zhiwang.planet.mvvm.model.entity.main.GameItem
import com.zhiwang.planet.ui.BaseActivity
import com.zhiwang.planet.ui.MembershipActivity
import com.zhiwang.planet.ui.WebViewActivity
import com.zhiwang.planet.ui.adapter.GameAdapter
import com.zhiwang.planet.ui.guessboard.RankActivity
import com.zhiwang.planet.ui.my.reward.RewardListActivity
import com.zhiwang.planet.util.*
import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.activity_game_base.*
import javax.inject.Inject

class GameBaseActivity : BaseActivity(), UserInfoView, VipView, GameListView {

    private var requestManager: RequestManager? = null

    @Inject
    lateinit var userPresenter: UserPresenter

    @Inject
    lateinit var vipPresenter: VipPresenter


    @Inject
    lateinit var scalarPresenter: ScalarPresenter


    var adapter: GameAdapter = GameAdapter()

    lateinit var disposable: Disposable

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_game_base)
        setSupportActionBar(toolbar)
        com.zhiwang.planet.util.Utils.setStatusBar(this, false, false)
        DaggerPayComponent.builder()
                .appComponent(getAppComponent())
                .accountModule(AccountModule(this))
                .autoDisposeModule(AutoDisposeModule(this))
                .build()
                .inject(this)
        init()
        userPresenter.userInfoView = this
        vipPresenter.vipView = this
        scalarPresenter.gameListView = this
        vipPresenter.getVipInfo()
        disposable = userPresenter.queryUserInfo()!!
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.game_base_menu, menu)
        return super.onCreateOptionsMenu(menu)

    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        if (item != null && item.itemId == R.id.guess_board) {
            quickStartActivity(RewardListActivity::class.java)
        }
        return super.onOptionsItemSelected(item)
    }

    fun init() {
        scalarPresenter.getGameList()
        gameRecyclerView.setHasFixedSize(true)
        gameRecyclerView.adapter = adapter

        openVip.setOnClickListener {
            quickStartActivity(MembershipActivity::class.java)
        }
        lottery.setOnClickListener {
            WebViewActivity.loadImmersive(this@GameBaseActivity, Store.URL_LOTTERY)
        }

        get_qudou.setOnClickListener {
            val alertDialog = AlertDialog.Builder(this@GameBaseActivity)
                    .setView(R.layout.game_base_get_bean_dialog_layout)
                    .create()

            alertDialog.setOnShowListener { dialogInterface ->
                alertDialog.findViewById<AppCompatTextView>(R.id.go_entertain)!!.setOnClickListener {
                    suicideAfterTransition()
                }

                alertDialog.findViewById<AppCompatTextView>(R.id.go_vip)!!.setOnClickListener {
                    quickStartActivity(MembershipActivity::class.java)
                    dialogInterface.dismiss()
                }

                alertDialog.findViewById<ImageView>(R.id.closeBtn)!!.setOnClickListener {
                    dialogInterface.dismiss()
                }
            }

            alertDialog.show()
        }

//        fast28_layout.setOnClickListener { WebViewActivity.load(this, Store.URL_FAST28) }
//
//        qq28_layout.setOnClickListener { goPc28(it) }

    }

    override fun onUserInfoLoad(user: User?) {
        if (user != null) {
            user_name.text = user.userName
            beanNumber.text = StringHolder.qudouNumber
            if (requestManager == null)
                requestManager = Glide.with(this@GameBaseActivity.applicationContext)
            requestManager?.load(com.zhiwang.planet.util.Utils.fixAvatarUrlPrefix(user.avatar))
                    ?.into(user_avatar)
        }
    }

    override fun onLoadVipInfo(vipModel: VipModel) {
        if ((vipModel.vipInfo.vipLevel == null) || (vipModel.vipInfo.vipLevel == "0")) {
//            user_name.setCompoundDrawables(null, null, null, null)
            iv_Vip.setGone()
        } else {
            iv_Vip.setVisible()
            Glide.with(this)
                    .load(com.zhiwang.planet.util.Utils.vipIcon(vipModel.vipInfo.vipLevel))
                    .into(iv_Vip)
        }
    }

    override fun onGameListLoad(gameList: List<GameItem>) {
        adapter.setDatas(gameList)
    }


    fun goPc28(v: View) {
        WebViewActivity.load(this, Store.URL_PC28)
    }

    fun goRank(v: View) {
        quickStartActivity(RankActivity::class.java)
    }

    override fun onSupportNavigateUp(): Boolean {
        onBackPressed()
        return true
    }

    override fun onDestroy() {
        disposable.dispose()
        super.onDestroy()
    }
}
